package com.example.administrator.q1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 */

public class MyPersonAdapter extends BaseAdapter implements View.OnClickListener{

    Context context;
    List<Person> list;
    LayoutInflater layoutInflater;
    private InnerItemOnclickListener mListener;

    interface InnerItemOnclickListener{
        void itemClick(View view);
    }

    public MyPersonAdapter(Context context, List<Person> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.list_view_person, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.sex = (TextView) view.findViewById(R.id.sex);
            viewHolder.align = (TextView) view.findViewById(R.id.align);
            viewHolder.HP = (TextView) view.findViewById(R.id.HP);
            viewHolder.Pw = (TextView) view.findViewById(R.id.Pw);
            viewHolder.AC = (TextView) view.findViewById(R.id.AC);
            viewHolder.Xp = (TextView) view.findViewById(R.id.Xp);
            viewHolder.Img = (ImageView) view.findViewById(R.id.Img_Icon);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // 要实现listView内部item的点击事件，就要在相应的Adapter里实现View.OnClickListener，给要点击事件
        // 的item设置onClickLister并且设置TAG用以区别


        viewHolder.name.setText(list.get(i).getName());
        viewHolder.name.setOnClickListener(this);
        viewHolder.name.setTag(i);
        viewHolder.sex.setText(list.get(i).getSex());
        viewHolder.sex.setOnClickListener(this);
        viewHolder.sex.setTag(i);
        viewHolder.align.setText(list.get(i).getAlign());
        viewHolder.align.setOnClickListener(this);
        viewHolder.align.setTag(i);
        viewHolder.HP.setText(list.get(i).getHP()+"");
        viewHolder.HP.setOnClickListener(this);
        viewHolder.HP.setTag(i);
        viewHolder.Pw.setText(list.get(i).getPw()+"");
        viewHolder.Pw.setOnClickListener(this);
        viewHolder.Pw.setTag(i);
        viewHolder.AC.setText(list.get(i).getAC()+"");
        viewHolder.AC.setOnClickListener(this);
        viewHolder.AC.setTag(i);
        viewHolder.Xp.setText(list.get(i).getXp()+"");
        viewHolder.Xp.setOnClickListener(this);
        viewHolder.Xp.setTag(i);
        viewHolder.Img.setImageResource(context.getResources().getIdentifier(list.get(i).getImg(),"drawable" ,context.getPackageName()));
        viewHolder.Img.setOnClickListener(this);
        viewHolder.Img.setTag(i);

        return view;
    }

    // String name, String sex, String align, int HP, int Pw, int AC, int Xp
    public final static class ViewHolder {
        TextView name;
        TextView sex;
        TextView align;
        TextView HP;
        TextView Pw;
        TextView AC;
        TextView Xp;
        ImageView Img;
    }

    public void setOnInnerItemOnclickListener(InnerItemOnclickListener listener) {
        this.mListener = listener;
    }

    public void onClick(View view) {
        mListener.itemClick(view);
    }
}
